start();

window.onload=function () {
    if(localStorage.getItem("chooseAnswer") !=null){
        var chooseAnswer=localStorage.getItem("chooseAnswer");
        chooseAnswer=JSON.parse(chooseAnswer);
        var t=chooseAnswer[choiceId];
        for(var i=0;i<icons.length;i++){

            if(icons[i].value == t){
                icons[i].parentElement.className="selected";
                icons[i].checked=true;
            }
        }
    }


    if(localStorage.getItem("shoucang")!=null){
        var shoucang=localStorage.getItem("shoucang");
        shoucang=JSON.parse(shoucang);
        if(shoucang[choiceId]=="已经收藏"){
            var collect=document.querySelector(".topic1");
            collect.style.color="#25BB9B";
            collect.innerText="已经收藏";
        }

    }
}
//选项的选中
/*var choiceDiv=document.querySelector(".selected");*/
var icons=document.querySelectorAll(".icons");
console.log(icons);


var boxArr=document.querySelectorAll(".boxs[type=checkbox]");
var thirdLine=document.querySelector(".third-line");
thirdLine.onclick=function(event){
    var ele=event.target;
    /*console.log(ele.className);
    console.log(ele.nodeName);*/
    console.log(ele);
    if(ele.className=="boxs" && ele.nodeName=="DIV"){
        var subjectOptionsArr=document.querySelectorAll(".subject-options");
        for(var i=0;i<subjectOptionsArr.length;i++){
            subjectOptionsArr[i].firstElementChild.className="boxs";
            ele.className="selected";
            ele.firstElementChild.checked=true;
            var chooseAnswer=localStorage.getItem("chooseAnswer");
            chooseAnswer=JSON.parse(chooseAnswer);

           /* var itemChange="";
            //答案转化
            switch(ele.children[0].value){
                case "item1":itemChange="a";break;
                case "item2":itemChange="b";break;
                case "item3":itemChange="c";break;
                case "item4":itemChange="d";break;
            }*/
           if(chooseAnswer==null)
               chooseAnswer={};//

            chooseAnswer[choiceId]=ele.children[0].value;

            localStorage.setItem("chooseAnswer",JSON.stringify(chooseAnswer));
        }
    }

    if(ele.className=="icons" && ele.nodeName=="INPUT"){
        var subjectOptionsArr=document.querySelectorAll(".subject-options")
        for(var i=0;i<subjectOptionsArr.length;i++){
            subjectOptionsArr[i].firstElementChild.className="boxs";
            ele.parentElement.className="selected";
            ele.checked=true;
        }
    }
}





//展开答题卡/收起答题卡
//展开答题卡
var openCard=document.querySelector(".open-card");
//收起答题卡
var closeCard=document.querySelector(".close-card");
//题目的数量
var boxOpen=document.querySelector(".box-open");

openCard.onclick= function () {
    console.log("-----------------");
    boxOpen.style.display="block";
    openCard.style.display="none";
    closeCard.style.display="block";
}
closeCard.onclick= function (){
    console.log("==================");
    console.log(closeCard);
    console.log(openCard);
    boxOpen.style.display="none";
    openCard.style.display="block";
    closeCard.style.display="none";
}




//下一题
var nextBtn=document.querySelector(".next-btn");
function next(questionSetId,choiceId){
    console.log("下一题");

    window.location.href="/helloworld/choose/chooseDetail/"+questionSetId+"/"+choiceId;
}

$.ajax({
    type:"GET",
    url:"/helloworld/choose/chooseCollect/"+customerId,
    datatype:"json",
    success:function(result){
        console.log(result);
    }
})

function choiceCollect(choiceId){
    //收藏
    var collect=document.querySelector(".topic1");

       // console.log("点击了收藏");
        if(collect.innerText=="收藏本题"){
            if(confirm("确定收藏该道题目吗？")){
                collect.innerText="已经收藏";
                collect.style.color="#25BB9B";

                var shoucang=localStorage.getItem("shoucang");
                shoucang=JSON.parse(shoucang);

                if(shoucang == null)
                    shoucang={};
                shoucang[choiceId]=collect.innerText;
                localStorage.setItem("shoucang",JSON.stringify(shoucang));

                var url="/helloworld/choose/chooseCollect/"+customerId+"/"+choiceId;

                $.ajax({
                    type:"GET",
                    url:url,
                    datatype:"json",
                    success:function (result) {
                        console.log(result);

                    }
                })
            }
        }else{
            if(confirm("确定取消收藏该道题目吗？")){
                collect.style.color="#333333";
                collect.innerText="收藏本题";

                var url="/helloworld/choose/chooseCollect/"+customerId+"/"+choiceId;

                $.ajax({
                    type:"GET",
                    url:url,
                    datatype:"json",
                    success:function (result) {
                        console.log(result);

                    }
                })
            }
        }
}
/*//标记
var mark=document.querySelector(".topic2");
mark.onclick=function(){
    console.log("点击了标记");
    if(mark.innerText=="标记一下") {
        mark.style.color = "#25BB9B";
        mark.innerText = "已经标记";
    }else if(mark.innerText=="已经标记"){
        mark.style.color = "#333333";
        mark.innerText = "标记一下";
    }
}*/

var ten=document.querySelector(".number-one");
console.log(ten.innerText);
if(ten.innerText == 10){
    var submitBtn=document.querySelector(".submit-btn");
    submitBtn.style.display="none";
    var finishBtn=document.querySelector(".finish-btn");
    finishBtn.style.display="block";
}




var allChoose={};
var allItem=[];



//交卷
var finishBtn=document.querySelector(".finish-btn");
function submit(questionSetId){

    var time=(localStorage.getItem("hour")*60*60*1000+localStorage.getItem("minute")*60*1000+localStorage.getItem("second")*1000);
    $.ajax({
        type:"POST",
        url:"/helloworld/choose/chooseDetail/answer/"+questionSetId+"/"+time,
        data: localStorage.getItem("chooseAnswer"),
        headers:{'Content-Type':'application/json;charset=utf8'},//415错误
        datatype:"json",
        success:function (result) {
            console.log(result);
            window.location.href="/helloworld/choose/chooseDetail/answer/"+questionSetId;
        }
    })


    timeEnd();
    var timeLong=document.querySelector(".time-long");
    console.log("时长为："+timeLong.value);
    localStorage.clear(); //提交后清空本地缓存
}
//计时器
var hour,minute,second;//时 分 秒
hour=localStorage.getItem("hour")||0;
minute=localStorage.getItem("minute")||0;
second=localStorage.getItem("second")||0;
var int;
function start()//开始
{
    int=setInterval(timeBegin,1000);
}
//开始计时
function timeBegin() {

    second++;
    if(second>=60)
    {
        second=0;
        minute++;
    }

    if(minute>=60)
    {
        minute=0;
        hour++;
    }

    localStorage.setItem("hour",hour);
    localStorage.setItem("minute",minute);
    localStorage.setItem("second",second);
    document.getElementById('timetext').value=hour+'时'+minute+'分'+second+'秒';

}
//暂停并且清空计时器
function timeEnd() {
    window.clearInterval(int);
}
/*//重置
oReset.onclick= function () {
    oTxt.value="00:00:00";
    n=0;
};*/
//补零
function toDub(n){
    return n<10?"0"+n:""+n;
}


//把答案数据存进localstorage里面
/*
localStorage.setItem("tom",23);
console.log(localStorage.getItem("tom"));
*/


